<template>
	<jk-dialog
		v-model="showDialog"
		:title="`选择-${title}`"
		append-to-body
		width="1000px"
		@on-visible-change="onVisibleChange"
		@on-cancel="onVisibleChange(false)"
		@on-confirm="onConfirmBtn"
	>
		<jk-dialog
			v-model="innerVisible"
			append-to-body
			title="选择-批号"
			width="30%"
			@on-cancel="innerVisible = false"
			@on-confirm="onBatchCodeConfirmBtn"
		>
			<el-select
				v-model="batchCode"
				class="width-100-percent"
				placeholder="生产批号"
			>
				<el-option
					v-for="item in productOrderBatchList"
					:key="item.batchCode"
					:label="item.batchCode"
					:value="item.batchCode"
				></el-option>
			</el-select>
		</jk-dialog>
		<header ref="box1" class="margin-bottom-4 flex-end-center">
			<!--			<jk-search-input-->
			<!--				v-model="searchParams.customerName"-->
			<!--				class="search-bar-item-size3 margin-5-0 item-gap"-->
			<!--				clearable-->
			<!--				placeholder="客户名称"-->
			<!--				size="small"-->
			<!--				@on-enter="onSearchBtn"-->
			<!--			/>-->
			<jk-search-input
				v-model="searchParams.code"
				class="search-bar-item-code margin-5-0 item-gap"
				clearable
				placeholder="生产订单号"
				size="small"
				@on-enter="onSearchBtn"
			/>
			<jk-search-input
				v-model="searchParams.productName"
				class="search-bar-item-size3 margin-5-0 item-gap"
				clearable
				placeholder="品种名称"
				size="small"
				@on-enter="onSearchBtn"
			/>
			<jk-select
				v-model="searchParams.serviceState"
				:option-list="orderStateList"
				class="search-bar-item-size2 margin-5-0 item-gap"
				clearable
				placeholder="状态"
				size="small"
				@change="onSearchBtn"
			></jk-select>

			<jk-search-button @click="onSearchBtn"></jk-search-button>
		</header>

		<jk-table
			ref="jkTable"
			:data="tableData"
			:height="dialogTableHeight - 30"
			:loading="tableLoading"
			:radio-config="{trigger: 'row'}"
			border
			class="width-100-percent"
			highlight-current-row
			highlight-hover-row
			@radio-change="onRadioChange"
			@cell-dblclick="onCellDblclick"
		>
			<vxe-table-column align="center" fixed="left" title="单选" type="radio" width="60"></vxe-table-column>
			<!--			<vxe-table-column :width="100" field="customerName" title="客户名称" />-->
			<vxe-table-column :min-width="200" field="code" title="生产订单号"></vxe-table-column>
			<vxe-table-column :min-width="120" field="productName" title="品种名称" />
			<vxe-table-column :min-width="120" field="productCode" title="品种编号" />
			<vxe-table-column :min-width="180" field="serviceState" title="状态">
				<template slot-scope="scope">
					<jk-state-bar :state="scope.row.serviceState" :state-list="orderStateList"></jk-state-bar>
				</template>
			</vxe-table-column>
		</jk-table>
		<footer ref="box2" class="flex-end-center margin-top-10">
			<jk-page
				:page-index="searchParams.pageNo"
				:page-size="searchParams.pageSize"
				:page-sizes="searchParams.pageSizes"
				:total="totalCount"
				layout="total, prev, pager, next"
				@on-change="onPageChange"
			/>
		</footer>
	</jk-dialog>
</template>

<script>
    import calDialogHeight from '@/mixins/cal-dialog-height';
    import { createTree, firstCharacterToLowerCase, pageConfig } from '@/utils/jk/jk-tool';
    import { spinProductPage } from '@/api/material/material';
    import { spinProductCategoryList } from '@/api/material/category';
    import { systemBusinessDictPage } from '@/api/basicData/businessDict';
    import { productionSaleOrderPage } from '@/api/production/order';

    import {
        productionPrdOrderPage
    } from '/src/api/production/order';
    import {
        orderStateList
    } from '@/utils/jk/status-option-list';

    import { systemUserColumnConfigGet } from '@/api/system/column-config';
    import processSteps from '@/views/production/order/components/process-steps/index.vue';
    import jkProgress from '@/views/jk-components/jk-progress/index.vue';

    export default {
        components: { jkProgress, processSteps },
        mixins: [calDialogHeight],
        props: {
            dialogState: {
                type: Boolean,
                default: false
            },
            title: {
                type: String,
                default: '生产订单'
            },
            formData: {
                type: Object,
                default() {
                    return {};
                }
            }
        },
        data() {
            return {
                totalCount: 0,
                dialogVisible: false,
                innerVisible: false,
                batchCode: '',
                orderStateList: orderStateList,
                searchParams: {
                    pageNo: pageConfig.pageNo,
                    pageSize: pageConfig.pageSize,
                    customerName: '',
                    code: '',
                    serviceState: '', // 全部
                    productName: '',
                    batchCode: ''
                },

                productOrderBatchList: [],
                tableColumnList: [],
                processList: [],
                categoryIds: [],

                typeList: [],
                treeData: [],
                selectRow: null,
                searchBarProcessId: null,
                searchBarNameOrCode: '',
                tableData: [],
                showDialog: false,
                tableLoading: false,
                selectData: []
            };
        },
        methods: {
            // getMenuColumnListRequest() {
            //     return systemUserColumnConfigGet({
            //         path: 'prdOrder',
            //         filterShow: true
            //     }).then(res => {
            //         if (res.data.status === 200) {
            //             this.tableColumnList = res.data.res.columnConfigList && res.data.res.columnConfigList.length ? res.data.res.columnConfigList[0].itemList : [];
            //         }
            //     });
            // },

            onMenuChange(e) {
                if (e) {
                    this.$set(this.searchParams, 'categoryId', e[e.length - 1]);
                } else {
                    this.$set(this.searchParams, 'categoryId', null);
                }
                this.getListRequest();
            },
            onCellDblclick(e) {
                this.$emit('on-confirm', e);
                // this.$emit('on-confirm', e.row);
            },
            onRadioChange(selection) {
                this.selectData = selection;

                // this.selectData = selection.row;
            },
            checkboxMethod({ row }) {
                return row._disabled !== true;
            },
            onSearchBtn() {
                this.$set(this.searchParams, 'pageNo', 1);
                this.getListRequest();
            },
            onPageChange(e, size) {
                this.$set(this.searchParams, 'pageNo', e);
                this.$set(this.searchParams, 'pageSize', size);
                this.getListRequest();
            },
            onBatchCodeConfirmBtn() {
                console.log(this.batchCode, 'this.batchCode');
                this.innerVisible = false;
                // this.showDialog = false;
                this.selectData.row.productOrderBatchList[0].batchCode = this.batchCode;
                this.$emit('on-confirm', this.selectData);
            },
            onConfirmBtn() {
                // 这里还需要判断他选择的是几个批号,如果是一个批号默认只选择第一个
                if (this.selectData.row) {
                    if (this.selectData.row.productOrderBatchList.length > 1) {
                        this.innerVisible = true;
                        this.productOrderBatchList = this.selectData.row.productOrderBatchList;
                    } else {
                        // 只有一条不打开选择批号的弹窗,并且默认选择第一个批号
                        this.$emit('on-confirm', this.selectData);
                    }
                } else {
                    this.$tipsBar(this, 'checkbox');
                }

                // if (Object.keys(this.selectData).length) {
                //     this.$emit('on-confirm', this.selectData);
                // } else {
                //     this.$tipsBar(this, 'unChoose');
                // }
            },
            getListRequest() {
                this.tableLoading = true;
                this.selectData = [];
                return productionPrdOrderPage(this.searchParams).then(res => {
                    this.tableLoading = false;
                    if (res.data.status === 200) {
                        this.tableData = res.data.res;
                        this.totalCount = res.data.count;
                    }
                });
            },
            onVisibleChange(e) {
                this.$emit('on-visible-change', e);
                if (!e) {
                    Object.assign(this.$data, this.$options.data());
                }
            },
            // getTreeListRequest() {
            //     return spinProductCategoryList().then(res => {
            //         if (res.data.status === 200) {
            //             const resData = res.data.res ? JSON.parse(JSON.stringify(res.data.res)) : [];
            //             if (this.defaultCategory) {
            //                 // const ids = resData.find(x => x.name === this.defaultCategory) ? resData.find(x => x.name === this.defaultCategory).id : null;
            //                 // this.$set(this.searchParams, 'categoryId', ids);
            //                 // this.categoryIds = [ids];
            //             }
            //             this.treeData = createTree(resData, {});
            //         }
            //     });
            // },
            // getStockTypeListRequest() {
            //     return systemBusinessDictPage({ categoryCode: 'STOCK_TYPE' }).then(res => {
            //         if (res.data.status === 200) {
            //             this.typeList = res.data.res;
            //         }
            //     });
            // },
            async getDependentData() {
                this.tableLoading = true;
                // this.$set(this.searchParams, 'workshopId', this.formData.workshopId);
                // this.$set(this.searchParams, 'typeNameOrCode', this.defaultStockType);
                // await this.getStockTypeListRequest();
                // await this.getTreeListRequest();
                await this.getListRequest();
                // await this.getMenuColumnListRequest();
                // await this.getMenuColumnListRequest();
            }
        },
        watch: {
            dialogState(newVal) {
                this.showDialog = newVal;
                if (newVal) {
                    this.getDependentData();
                }
            }
        }
    };
</script>
